<?php
namespace Home\Model;
use Think\Model;
class GoodsModel extends Model {
    public function getrecgoods($recid,$limit){
		$goodsid=D('recvalue')->field('valueid')->where(array('recid'=>$recid))->limit($limit)->select();
		$recgoods=array();
		foreach($goodsid as $k=>$v){
			$recgoods[]=D('goods')->find($v['valueid']);
		}
		return $recgoods;
	}
	
	
	//取得当前商品的所有会员价格
	public function levelprice($goodid){
		$memberLevel=D('memberLevel')->select();
		$shopPrice=D('goods')->field('shop_price')->find($goodid);
		
		$levelPrice=array();
		foreach($memberLevel as $k =>$v){
			$memberPrice=D('memberPrice')->field('price')->where(array('goods_id'=>$goodid,'lever_id'=>$v['id']))->find();
			$memberPrice=$memberPrice['price'];
			if($memberPrice != null){
					$levelPrice[$k]['price']=$memberPrice;
					$levelPrice[$k]['levelname']=$v['level_name'];
			}else{
					$levelPrice[$k]['price']=($shopPrice['shop_price']*$v['rate'])/100;
				
					$levelPrice[$k]['levelname']=$v['level_name'];
			}	
		}
		return $levelPrice;
	}
	
	
	
	//返回当前商品当前登录会员的商品价格
	public function getMemberPrice($goodsId){
		$rate=session('rate');
		$levelId=session('levelId');
	
		if($levelId){
			$mprice=D('memberPrice')->field('price')->where(array('lever_id'=>$levelId,'goods_id'=>$goodsId))->find();
			if($mprice){
				$mprice=$mprice['price'];
			
			}else{
				$shopPrice=$this->field('shop_price')->find($goodsId);
				$shopprice=$shopPrice['shop_price'];
				$mprice=($shopprice*$rate)/100;
			
			}
			
		}else{
			$shopPrice=$this->field('shop_price')->find($goodsId);
			$mprice=$shopPrice['shop_price'];
			
		}
		return $mprice;
	}
	
	public function getGoodsNum($gid,$gaid){
		$product = D('product');
		$product->field('goods_number')->where(array('goods_id'=>$gid,'goods_attr'=>$gaid))->find();
		return $product->goods_number;
	}
	
}